package lucky.xiong.煞笔面试题.JUC.threadLocal.线程池异常处理;

import java.util.concurrent.*;

/**
 * @author XiongJianLin
 * @date Created in 2024/12/6 16:47
 */
public class PollException {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        submit();
        handleException();

     }

    /**
     * 默认线程使用和异常抛出情况
     * @throws ExecutionException
     * @throws InterruptedException
     */
    public static void submit() throws ExecutionException, InterruptedException {
        ExecutorService threadPool = Executors.newFixedThreadPool(3);

        try {
            // 提交任务方式一
            // submit 会吞掉异常
            threadPool.submit(() -> {
                System.out.println("hello");
                int i = 10 / 0;
                System.out.println("不嘻嘻");


            });
            // 方式二： submit() 获取返回值 ， 调用 get()会抛出异常
            Future<?> world = threadPool.submit(() -> {
                System.out.println("返回555");
//                return 555 ;
                int i = 10 / 0;
                System.out.println("不55");
            });
            // 如果调用了get()就会抛出
//            world.get();
            System.out.println();


            // 方式三 ：  默认会抛出异常
            threadPool.execute(() -> {
                System.out.println("666");
                System.out.println(10 / 0);
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

        threadPool.shutdown();
    }

    /**
     * 同时处理 submit() 不同异常抛出情况。
     */
    public static void handleException() {
        // 自定义线程池
        // ThreadPoolExecutor.afterExecute()  处理异常
//        ExecutorService threadPool = new ThreadPoolExecutor(2,
//                Runtime.getRuntime().availableProcessors(),
//                1,
//                TimeUnit.SECONDS,
//                new ArrayBlockingQueue<>(3));

        ExecutorService threadPool = new ThreadPoolExecutor(2,
                Runtime.getRuntime().availableProcessors(),
                1,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(3)){
            @Override
            protected void afterExecute(Runnable r, Throwable t) {
                // execute 运行
                if (t != null){
                    System.out.println();
                    t.printStackTrace();
                }
                // submit 运行
                if (t == null && r instanceof Future<?>){
                    Future<?> future = (Future<?>) r;
                    try {
                        if (future.isDone()){
                            future.get();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (ExecutionException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        threadPool.submit(()->{
            System.out.println("hello");
            System.out.println(10/0);
        });



    }

}
